<template>
  <div class="login-container">
    <div class="header">
      <!-- <img src="../../assets/index_images/images/login.png" width="320" alt=""> -->
    </div>
    <div class="center">
      <div class="center-bg"></div>
      <el-row >
        <el-col style="margin:30px 0"  class="hidden-md-and-up " :xs="24" :sm="24" :md="6" :lg="6" :xl="6">
          <div class="form">
            <el-form class="card-box login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
              <h3 class="title">用户登录</h3>

              <el-form-item prop="username">
                <span class="svg-container svg-container_login">
                  <svg-icon icon-class="user" />
                </span>
                <el-input name="username" size="mini" type="text" v-model="loginForm.userName" autoComplete="on" placeholder="用户名" />
              </el-form-item>

              <el-form-item prop="password">
                <span class="svg-container">
                  <svg-icon icon-class="password" />
                </span>
                <el-input name="password" size="mini"  :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on"
                  placeholder="密码" />
                <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye" /></span>
              </el-form-item>
              <el-row>
                <el-col :span="12">
                  <el-button type="primary" style="margin-bottom:30px;width:100%;" :loading="loading" @click.native.prevent="handleLogin">登录</el-button>
                </el-col>
                <el-col :span="12">
                  <el-button type="text" style="margin-bottom:30px;color:#fff;float:right;">忘记密码？</el-button>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </el-col>
        <el-col style="text-align:center;margin:30px 0" class="hidden-xs-only" :xs="24" :sm="24" :md="6" :lg="6" :xl="6">
          <img class="banner_img" src="../../assets/index_images/2.png"  alt="">
        </el-col>
        <el-col style="text-align:center;margin:30px 0" class="hidden-xs-only" :xs="24" :sm="24" :md="6" :lg="6" :xl="6">
          <img class="banner_img" src="../../assets/index_images/2.png"  alt="">
        </el-col>
        <el-col style="text-align:center;margin:30px 0" class="hidden-xs-only" :xs="24" :sm="24" :md="6" :lg="6" :xl="6">
          <img class="banner_img" src="../../assets/index_images/3.png"  alt="">
        </el-col>
        <el-col style="margin:30px 0" class="hidden-xs-only hidden-sm-only" :xs="24" :sm="24" :md="6" :lg="6" :xl="6">
          <div class="form">
            <el-form class="card-box login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
              <h3 class="title">用户登录</h3>
              <el-form-item prop="username">
                <span class="svg-container svg-container_login">
                  <svg-icon icon-class="user" />
                </span>
                <el-input name="username" size="mini" type="text" v-model="loginForm.userName" autoComplete="on" placeholder="用户名" />
              </el-form-item>

              <el-form-item prop="password">
                <span class="svg-container">
                  <svg-icon icon-class="password" />
                </span>
                <el-input name="password" size="mini"  :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on"
                  placeholder="密码" />
                <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye" /></span>
              </el-form-item>
              <el-row>
                <el-col :span="12">
                  <el-button type="primary" style="margin-bottom:30px;width:100%;" :loading="loading" @click.native.prevent="handleLogin">登录</el-button>
                </el-col>
                <el-col :span="12">
                  <el-button type="text" style="margin-bottom:30px;color:#fff;float:right;">忘记密码？</el-button>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="footer">
      <div class="footer-sb">Copyright ©2018 在线小说管理系统  蜀ICP备17034570</div>
    </div>
  </div>
</template>
<script>
import { isvalidUsername } from '@/utils/validate'
import loginApi  from '@/api/login'
import { param2Obj } from '@/utils'
import 'element-ui/lib/theme-chalk/display.css';
export default {
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 3) {
        callback(new Error('密码不能小于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        userName: '',
        password: ''
      },
      loginRules: {
        userName: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      pwdType: 'password',
      loading: false,
      showDialog: false
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    handleLogin() {
      this.loading = true;
       this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: '/' })

          }).catch(() => {
            this.loading = false
          })
        } else {
          this.loading = false
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  created() {
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  @import "src/styles/mixin.scss";
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;

  .login-container {
    @include relative;
    // background-image:url('../../assets/index_images/login.jpg');
    //background-size:100% 100vh;
    .header{

      padding:15px 0;
      img{
        margin-left:20px;
      }
    }
    .center{
      position:relative;
      height:50vh;
      .center-bg{
        background-image:url(../../assets/index_images/login_img.jpg);
        padding:10px 0px;
        position:absolute;
        width:100%;
        height:100%;
        background-position: center center;
        background-size: cover;
      }
      .banner_img{
        width:90%;
        height:auto;
        //transform: translateY(50%);
        border:5px solid #fff;
      }
      .form{
        // position:absolute;
        // right:120px;
        // top:110px;
        input:-webkit-autofill {
      -webkit-box-shadow: 0 0 0px 1000px #293444 inset !important;
      -webkit-text-fill-color: #fff !important;
      }
        input {
          background-color:transparent;
          border: 0px;
          -webkit-appearance: none;
          border-radius: 0px;
          padding: 12px 5px 12px 15px;
          color: $light_gray;
          height: 30px;
        }
        .el-input {
          display: inline-block;
          height: 30px;
          width: 80%;
          background-color:transparent;
        }
        .tips {
          font-size: 14px;
          color: #fff;
          margin-bottom: 10px;
        }
        .svg-container {
          padding: 6px 5px 6px 15px;
          color: $dark_gray;
          vertical-align: middle;
          width: 30px;
          display: inline-block;
          &_login {
            font-size: 20px;
          }
        }
        .title {
          font-size: 26px;
          font-weight: 400;
          color: $light_gray;
          padding: 25px 0px;
          margin:0px;
          text-align: center;
          font-weight: bold;
        }
        .login-form {
          width: 90%;
          margin:0 auto;
          padding: 0 14px;
          background-color:rgba(255,255,255,.4);
          border-radius:8px;
        }
        .el-form-item {
          border: 1px solid rgba(255, 255, 255, 0.1);
          background-color:rgba(255,255,255,.8);
          border-radius: 5px;
          color: #454545;
        }
        .el-input__inner{
          color:#000;
          background-color:transparent;
        }
        .el-form-item__content{
          line-height:30px;
        }
        .show-pwd {
          position: absolute;
          right: 10px;
          top: 7px;
          font-size: 16px;
          color: $dark_gray;
          cursor: pointer;
          user-select:none;
        }
        .thirdparty-button{
          position: absolute;
          right: 35px;
          bottom: 28px;
        }
      }
    }
    .footer{
      height:140px;
      position:relative;
      text-align:center;
      .footer-sb{
        margin-top: 50px;
        padding:10px 0;
        color:#666;
        text-align:center;
        font-size: 12px;
        line-height:60px;
        background-color:#e1e1e1;
      }
      .footer-bg{
        padding:10px 0px;
        width:90%;
        margin:0 auto;
      }
    }
  }
</style>
